package com.lc.luzhengtang.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.activity.ShopDetailActivity;
import com.lc.luzhengtang.bean.ShopCarListBean;
import com.lc.luzhengtang.conn.Conn;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.util.List;


/**
 * Created by AYD on 2016/11/21.
 * <p/>
 * 购物车Adapter
 */
public class ShoppingCartAdapter3 extends BaseAdapter implements View.OnClickListener {

    private boolean isShow = true;//是否显示编辑/完成
    private List<ShopCarListBean.DataBeanX.DataBean> shoppingCartBeanList;
    private CheckInterface checkInterface;
    private ModifyCountInterface modifyCountInterface;
    private Context context;
    private OnChooseClickListener onChooseClickListener;


    private OnNumChangListener onNumChangListener;

    public void setOnNumChangListener(OnNumChangListener onNumChangListener) {
        this.onNumChangListener = onNumChangListener;
    }

    public ShoppingCartAdapter3(Context context, List<ShopCarListBean.DataBeanX.DataBean> shoppingCartBeanList) {
        this.context = context;
        this.shoppingCartBeanList = shoppingCartBeanList;
    }

    public interface OnChooseClickListener {
        void onChooseClick(View view);
    }

    public void setOnChooseClickListener(OnChooseClickListener onChooseClickListener) {
        this.onChooseClickListener = onChooseClickListener;
    }

    /**
     * 单选接口
     *
     * @param checkInterface
     */
    public void setCheckInterface(CheckInterface checkInterface) {
        this.checkInterface = checkInterface;
    }

    /**
     * 改变商品数量接口
     *
     * @param modifyCountInterface
     */
    public void setModifyCountInterface(ModifyCountInterface modifyCountInterface) {
        this.modifyCountInterface = modifyCountInterface;
    }

    @Override
    public int getCount() {
        return shoppingCartBeanList == null ? 0 : shoppingCartBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return shoppingCartBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    /**
     * 是否显示可编辑
     *
     * @param flag
     */
    public void isShow(boolean flag) {
        isShow = flag;
        notifyDataSetChanged();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_shopping_cart_layout, parent, false);
          /*  convertView = BoundViewHelper.boundView(this, ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) LayoutInflater.from(context)
                    .inflate(R.layout.item_shopping_cart_layout, null)));*/
        /*    convertView = BoundViewHelper.boundView(this, LayoutInflater.from(context)
                    .inflate(R.layout.item_shopping_cart_layout, null));*/
//            convertView=LayoutInflater.from(context).inflate(R.layout.item_shopping_cart_layout2,null);
//            ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) convertView);

            holder = new ViewHolder(convertView);
            convertView.setTag(holder);


        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) convertView);
//        final ShoppingCartBean shoppingCartBean = shoppingCartBeanList.get(position);
        final ShopCarListBean.DataBeanX.DataBean shoppingCartBean = shoppingCartBeanList.get(position);
        holder.tv_commodity_name.setText(shoppingCartBean.getTitle());
        holder.view_lines.setTag(position);

        holder.tv_pants.setText("￥" + shoppingCartBean.getSales());

        holder.ck_chose.setChecked(shoppingCartBean.isChoosed());

        holder.tv_goods_num.setText(shoppingCartBean.getNumber() + "");

        Glide.with(context).load(Conn.HOST2 + shoppingCartBean.getPicurl()).crossFade().into(holder.iv_show_pic);

        holder.ib_up.setTag(position);
        holder.ib_up.setOnClickListener(this);
        holder.img_delete.setTag(position);
        holder.img_delete.setOnClickListener(this);

        holder.ib_up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onNumChangListener != null) {
                    onNumChangListener.onAddClick(shoppingCartBean);

                }
            }
        });

        holder.ib_down.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onNumChangListener != null) {
                    onNumChangListener.onSubClick(shoppingCartBean);

                }
            }
        });

//列表中删除数据呵呵哒
  /*      holder.img_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });*/

        //增加

        holder.ib_down.setTag(position);

        holder.ib_down.setOnClickListener(this);

        holder.ck_chose.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Log.d("1234", "isChecked = " + isChecked);
                holder.ib_down.setClickable(isChecked);
                holder.ib_up.setClickable(isChecked);
                holder.ib_down.setEnabled(isChecked);
                holder.ib_up.setEnabled(isChecked);
            }
        });

        //单选框按钮
        holder.ck_chose.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        shoppingCartBean.setChoosed(((CheckBox) v).isChecked());


                        checkInterface.checkGroup(position, ((CheckBox) v).isChecked());//向外暴露接口
                    }
                }
        );

        //判断是否在编辑状态下
        if (isShow) {
            holder.tv_commodity_name.setVisibility(View.VISIBLE);
//            holder.tv_fabric.setVisibility(View.VISIBLE);
//            holder.rl_edit.setVisibility(View.GONE);
//            holder.tv_delete.setVisibility(View.GONE);
        } else {
            holder.tv_commodity_name.setVisibility(View.GONE);
            //  holder.tv_fabric.setVisibility(View.GONE);
            //          holder.rl_edit.setVisibility(View.VISIBLE);
//            holder.tv_delete.setVisibility(View.VISIBLE);
        }

        holder.layout_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("goods_id", shoppingCartBean.getGoods_id() + "");

                intent.setClass(context, ShopDetailActivity.class);
                context.startActivity(intent);
            }
        });


        return convertView;
    }

    @Override
    public void onClick(View v) {
        onChooseClickListener.onChooseClick(v);
    }


    //初始化控件
    class ViewHolder {
        ImageView iv_chose;
        ImageView iv_show_pic, iv_sub, iv_add;
        TextView tv_commodity_name, tv_pants, tv_show_num;
        CheckBox ck_chose;
        RelativeLayout rl_edit;

        TextView tv_goods_num;
        //删除按钮
        ImageView img_delete;
        //增加按钮

        ImageButton ib_up;
        ImageButton ib_down;


        RelativeLayout layout_item;
        //下边的分割线
        View view_lines;

        public ViewHolder(View itemView) {
            ck_chose = (CheckBox) itemView.findViewById(R.id.ck_chose);
            iv_show_pic = (ImageView) itemView.findViewById(R.id.iv_show_pic);

            tv_commodity_name = (TextView) itemView.findViewById(R.id.tv_commodity_name);

            tv_pants = (TextView) itemView.findViewById(R.id.tv_pants);
            tv_goods_num = (TextView) itemView.findViewById(R.id.tv_goods_num);

            img_delete = (ImageView) itemView.findViewById(R.id.img_delete);

            //增加的
            ib_up = (ImageButton) itemView.findViewById(R.id.ib_up);
            //减少的button
            ib_down = (ImageButton) itemView.findViewById(R.id.ib_down);
            //数量
            tv_goods_num = (TextView) itemView.findViewById(R.id.tv_goods_num);
            layout_item = (RelativeLayout) itemView.findViewById(R.id.layout_item);
            view_lines = itemView.findViewById(R.id.view_lines);

            ib_down.setClickable(false);
            ib_down.setEnabled(false);
            ib_up.setClickable(false);
            ib_up.setEnabled(false);
        }

    }


    /**
     * 复选框接口
     */
    public interface CheckInterface {
        /**
         * 组选框状态改变触发的事件
         *
         * @param position  元素位置
         * @param isChecked 元素选中与否
         */
        void checkGroup(int position, boolean isChecked);
    }


    /**
     * 改变数量的接口
     */
    public interface ModifyCountInterface {
        /**
         * 增加操作
         *
         * @param position      组元素位置
         * @param showCountView 用于展示变化后数量的View
         * @param isChecked     子元素选中与否
         */
        void doIncrease(int position, View showCountView, boolean isChecked);

        /**
         * 删减操作
         *
         * @param position      组元素位置
         * @param showCountView 用于展示变化后数量的View
         * @param isChecked     子元素选中与否
         */
        void doDecrease(int position, View showCountView, boolean isChecked);

        /**
         * 删除子item
         *
         * @param position
         */
        void childDelete(int position);
    }

    public interface OnNumChangListener {
        void onAddClick(ShopCarListBean.DataBeanX.DataBean dataBean);

        void onSubClick(ShopCarListBean.DataBeanX.DataBean dataBean);
    }
}
